<?php

namespace App\Models;

use App\Traits\InsertOnDuplicateKey;
use Illuminate\Database\Eloquent\Model;
use Illuminate\Support\Facades\DB;
use App\Models\ZPituitaryAnswerLog;

/**
 * Class ZMmcAnswer
 * @package App\Models
 */
class ZPituitaryAnswer extends Model
{
    use InsertOnDuplicateKey;
    protected $table = 'z_pituitary_answer';
    protected $guarded = ['id'];

    const CREATED_AT = 'create_at';
    const UPDATED_AT = 'update_at';




    /**
     * 批量插入数据，同时对比新旧数据，同样的不更新，的更新，没有的删除，记录日志
     * @param $oldData array 旧数据
     * @param $newData array 新数据
     * @param $userId int
     * @param $visitLevel int
     * @param $templateId int
     * @return mixed
     */
    public static function insertWithCompareData($oldData, $newData, $userId, $visitLevel, $templateId, $docId = 0)
    {
        if ($oldData) {
            $delete = [];
            $newDataKey = $newData;
            if(in_array($templateId, [17,18,19,20])){
                //按照分组+answer_id
                $oldDataIds = [];
                $newDataAnswerIds = [];
                foreach($oldData as $item){
                    $index = $item["answer_id"]."_".$item["group_id"] ;
                    $oldDataIds[$index] = $item;
                }

                foreach($newData as $item){
                    $index = $item["answer_id"]."_".$item["group_id"] ;
                    $newDataAnswerIds[$index] = $item;
                }

            }else{
                 $oldDataIds = array_column($oldData, null, 'answer_id');
                 $newDataAnswerIds = array_column($newData, null, 'answer_id');
            }


            foreach ($oldDataIds as $k => $v) {
                if (isset($newDataAnswerIds[$k])) {
//                    if ($newDataAnswerIds[$k]['answer_name'] == $v['answer_name']) {
//                        unset($newData[array_search($v['answer_id'], array_column($newDataKey, 'answer_id'))]);
//                    }
                } else {
                    $delete[] = $v['id'];
                }
            }
            if ($delete) {
                ZPituitaryAnswer::destroy($delete);
            }
        }
        ZPituitaryAnswerLog::create([
            'doc_id'      => $docId,
            'user_id'     => $userId,
            'visit_level' => $visitLevel,
            'template'    => $templateId,
            'data'        => json_encode(['before' => $oldData, 'after' => $newData], 256)
        ]);
        if (count($newData) > 0) {
            $newData = array_merge($newData);
            ZPituitaryAnswer::insertOnDuplicateKey($newData, ['doc_id', 'answer_name', 'unit', 'reference', 'date', 'group_id', 'datasource', 'update_at']);
        }
    }

    /**
     * 计算章节完成度
     * @param $data array 提交数据
     * @return null
     */
    public static function complete($userId)
    {
        $data = ZPituitaryAnswer::query()->where('user_id', $userId)->get();
        $dataarr = array();
        foreach ($data as $k=>$v){
            $dataarr[$v['subquestion_id']] = $v;
        }
        $complete = 0;
        //选择第一题
        if (!empty($dataarr['138']['answer_name'])){
            //选择否
            if ($dataarr['138']['answer_name'] == '否'){
                $complete += 100;
            }else{
                //选择是
                $complete += 50;
                //时间是否填写
                if (!empty($dataarr['13801001']['answer_name'])){
                    $complete += 25;
                }
                //第二题是否填写
                if (!empty($dataarr['209']['answer_name'])){
                    $complete += 25;
                }
            }
        }
        //选择第三题
        if (!empty($dataarr['139']['answer_name'])){
            //选择否
            if ($dataarr['139']['answer_name'] == '否'){
                $complete += 100;
            }else{
                //选择是
                $complete += 50;
                //时间是否填写
                if (!empty($dataarr['13901001']['answer_name'])){
                    $complete += 25;
                }
                //第四题是否填写
                if (!empty($dataarr['210']['answer_name'])){
                    $complete += 25;
                }
            }
        }
        //选择第五题
        if (!empty($dataarr['140']['answer_name'])){
            //选择否
            if ($dataarr['140']['answer_name'] == '否'){
                $complete += 100;
            }else{
                //选择是
                $complete += 50;
                //时间是否填写
                if (!empty($dataarr['14001001']['answer_name'])){
                    $complete += 25;
                }
                //第六题是否填写
                if (!empty($dataarr['211']['answer_name'])){
                    $complete += 25;
                }
            }
        }
        //选择第七题
        if (!empty($dataarr['212']['answer_name'])){
            //选择否
            if ($dataarr['212']['answer_name'] == '否'){
                $complete += 100;
            }else{
                //选择是
                $complete += 50;
                //时间是否填写
                if (!empty($dataarr['21201001']['answer_name'])){
                    $complete += 25;
                }
                //第八题是否填写
                if (!empty($dataarr['213']['answer_name'])){
                    $complete += 25;
                }
            }
        }
        //选择第九题
        if (!empty($dataarr['141']['answer_name'])){
            //选择否
            if ($dataarr['141']['answer_name'] == '否'){
                $complete += 100;
            }else{
                //选择是
                $complete += 50;
                //时间是否填写
                if (!empty($dataarr['14101001']['answer_name'])){
                    $complete += 50;
                }
            }
        }
        //选择第十题
        if (!empty($dataarr['214']['answer_name'])){
            //选择否
            if ($dataarr['214']['answer_name'] == '否'){
                $complete += 100;
            }else{
                //选择是
                $complete += 50;
                //时间是否填写
                if (!empty($dataarr['21401001']['answer_name'])){
                    $complete += 12.5;
                }
                //11题是否填写
                if (!empty($dataarr['215']['answer_name'])){
                    $complete += 12.5;
                }
                //12题是否填写
                if (!empty($dataarr['216']['answer_name'])){
                    $complete += 12.5;
                }
                //13题是否填写
                if (!empty($dataarr['217']['answer_name'])){
                    $complete += 12.5;
                }
            }
        }
        //选择第十四题
        if (!empty($dataarr['218']['answer_name'])){
            //选择否
            if ($dataarr['218']['answer_name'] == '否'){
                $complete += 100;
            }else{
                //选择是
                $complete += 50;
                //时间是否填写
                if (!empty($dataarr['21801001']['answer_name'])){
                    $complete += 25;
                }
                //15题是否填写
                if (!empty($dataarr['219']['answer_name'])){
                    $complete += 12.5;
                }
                //16题是否填写
                if (!empty($dataarr['220']['answer_name'])){
                    $complete += 12.5;
                }
            }
        }
        //选择第十七题
        if (!empty($dataarr['221']['answer_name'])){
            //选择否
            if ($dataarr['221']['answer_name'] == '否'){
                $complete += 100;
            }else{
                //选择是
                $complete += 50;
                //发作次数是否填写
                if (!empty($dataarr['22101001']['answer_name'])){
                    $complete += 50;
                }
            }
        }
        //选择第十八题
        if (!empty($dataarr['222']['answer_name'])){
            //选择否
            if ($dataarr['222']['answer_name'] == '否'){
                $complete += 100;
            }else{
                //选择是
                $complete += 50;
                //时间是否填写
                if (!empty($dataarr['22201001']['answer_name'])){
                    $complete += 50;
                }
            }
        }
        //选择第十九题
        if (!empty($dataarr['223']['answer_name'])){
            //选择否
            if ($dataarr['223']['answer_name'] == '否'){
                $complete += 100;
            }else{
                //选择是
                $complete += 50;
                //时间是否填写
                if (!empty($dataarr['22301001']['answer_name'])){
                    $complete += 12.5;
                }
                //20题是否填写
                if (!empty($dataarr['224']['answer_name'])){
                    $complete += 12.5;
                }
                //21题是否填写
                if (!empty($dataarr['225']['answer_name'])){
                    $complete += 12.5;
                }
                //22题是否填写
                if (!empty($dataarr['226']['answer_name'])){
                    $complete += 12.5;
                }
            }
        }
        //选择第二十三题
        if (!empty($dataarr['146']['answer_name'])){
            //选择否
            if ($dataarr['146']['answer_name'] == '否'){
                $complete += 100;
            }else{
                //选择是
                $complete += 50;
                //时间是否填写
                if (!empty($dataarr['14601001']['answer_name'])){
                    $complete += 25;
                }
                //名称是否填写
                if (!empty($dataarr['14601002']['answer_name'])){
                    $complete += 25;
                }
            }
        }
        //选择第二十四题
        if (!empty($dataarr['147']['answer_name'])){
            //选择否
            if ($dataarr['147']['answer_name'] == '否'){
                $complete += 100;
            }else{
                //选择是
                $complete += 50;
                //时间是否填写
                if (!empty($dataarr['14701001']['answer_name'])){
                    $complete += 25;
                }
                //名称是否填写
                if (!empty($dataarr['14701002']['answer_name'])){
                    $complete += 25;
                }
            }
        }
        //选择第二十五题
        if (!empty($dataarr['148']['answer_name'])){
            //选择否
            if ($dataarr['148']['answer_name'] == '否'){
                $complete += 100;
            }else{
                //选择是
                $complete += 50;
                //时间是否填写
                if (!empty($dataarr['14801001']['answer_name'])){
                    $complete += 25;
                }
                //名称是否填写
                if (!empty($dataarr['14801002']['answer_name'])){
                    $complete += 12.5;
                }
                //26题.是否填写
                if (!empty($dataarr['149']['answer_name'])){
                    $complete += 12.5;
                }
            }
        }
        //选择第二十七题
        if (!empty($dataarr['150']['answer_name'])){
            //选择否
            if ($dataarr['150']['answer_name'] == '否'){
                $complete += 100;
            }else{
                //选择是
                $complete += 50;
                //时间是否填写
                if (!empty($dataarr['15001001']['answer_name'])){
                    $complete += 25;
                }
                //名称是否填写
                if (!empty($dataarr['15001002']['answer_name'])){
                    $complete += 25;
                }
            }
        }
        return round($complete/14);
    }
}
